package com.fastplatform.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.http.ResponseEntity;
import com.fastplatform.entity.Role;
import com.fastplatform.service.RoleService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.fastplatform.dto.RoleIdRequest;
import com.fastplatform.dto.AssignRoleMenusRequest;

@RestController
@RequestMapping("/role")
@CrossOrigin(origins = "*")
@SaCheckLogin
public class RoleController {
    
    @Autowired
    private RoleService roleService;
    
    /**
     * 获取角色列表
     */
    @GetMapping("/list")
    public ResponseEntity<List<Role>> getRoleList(
            @RequestParam(required = false) String roleName,
            @RequestParam(required = false) String roleCode,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            List<Role> roles = roleService.getRoleList(roleName, roleCode, status, page, size);
            return ResponseEntity.ok(roles);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }
    
    /**
     * 根据ID获取角色
     */
    @GetMapping("/get")
    public ResponseEntity<Role> getRoleById(@RequestParam Long id) {
        try {
            Role role = roleService.getRoleById(id);
            if (role == null) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(role);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }
    
    /**
     * 创建角色
     */
    @PostMapping("/create")
    public ResponseEntity<Role> createRole(@RequestBody Role role) {
        try {
            Role createdRole = roleService.createRole(role);
            return ResponseEntity.ok(createdRole);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }
    
    /**
     * 更新角色
     */
    @PostMapping("/update")
    public ResponseEntity<Role> updateRole(@RequestBody Role role) {
        try {
            Role updatedRole = roleService.updateRole(role);
            return ResponseEntity.ok(updatedRole);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }
    
    /**
     * 删除角色
     */
    @PostMapping("/delete")
    public ResponseEntity<String> deleteRole(@RequestBody RoleIdRequest request) {
        try {
            roleService.deleteRole(request.getId());
            return ResponseEntity.ok("删除成功");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 获取角色的菜单权限
     */
    @GetMapping("/menus")
    public ResponseEntity<List<Long>> getRoleMenus(@RequestParam Long id) {
        try {
            List<Long> menuIds = roleService.getRoleMenus(id);
            return ResponseEntity.ok(menuIds);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }
    
    /**
     * 分配角色菜单权限
     */
    @PostMapping("/menus")
    public ResponseEntity<String> assignRoleMenus(@RequestBody AssignRoleMenusRequest request) {
        try {
            roleService.assignRoleMenus(request.getId(), request.getMenuIds());
            return ResponseEntity.ok("权限分配成功");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
}